package org.rujue.common.config;

import org.rujue.modules.sys.interceptor.LogInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
	@Value("${adminPath}")
	private String adminPath;
	/**
	 * 跨域支持
	 */
	@Bean
	public WebMvcConfigurer corsConfigurer() {
		return new WebMvcConfigurer() {
			@Override
			public void addCorsMappings(CorsRegistry registry) {
				registry.addMapping("/**")
						.allowedMethods("*")
						.allowedHeaders("*")
						.allowedOriginPatterns("*")
						.allowCredentials(true)
						.maxAge(18000L);
			}
		};
	}
	
	@Override
    public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("forward:"+Global.getConfig("webIndex"));
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

	@Bean
	public LogInterceptor logInterceptor() {
		return new LogInterceptor();
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(logInterceptor())  // 添加拦截器
				.addPathPatterns(adminPath + "/**")          // 配置拦截请求url（ ** 表示拦截所有请求url）
				.excludePathPatterns(adminPath + "/login"); // 排除某些不需要拦截的请求url（即带有/hello请求不会被拦截）
	}
}
